import {
  HomeHttp
} from '../../../api/home.js'
import {
  KeywordHttp
} from '../../../api/keyword.js'

const homeModel = new HomeHttp()
const keywordModel = new KeywordHttp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    locationInfo: {}, // 定位信息
    storeId: wx.getStorageSync('store_id'), // 门店Id
    storeInfo: {}, // 当前门店信息
    searchVal: '', // 搜索值
    nearStreet: [], // 附近街道
    searchHistory: [], // 搜索历史
    searchList: [], // 搜索结果
    isShowSearchHistory: false, // 是否显示搜索历史
    isShowSearchResult: false, // 是否显示搜索结果
    isShowNull: false,
    isStoreInfo: true
  },

  initPage(options) {
    console.log(this.data.storeInfo);
    wx.hideShareMenu();
    this.httpWxGetLocation() // 定位
    this.setData({
      storeInfo: wx.getStorageSync('servicePointInfo_2'),
      searchHistory: keywordModel.getHistory('search_street_history')
    })
  },

  // 搜索框 - 获取焦点
  onSearchFocus() {
    this.setData({
      searchHistory: keywordModel.getHistory('search_street_history'),
      isShowSearchHistory: true,
      isShowSearchResult: false,
      isShowNull: false
    })
  },

  // 搜索框 - 失去焦点
  onSearchBlur() {
    if (this.data.searchVal === '') {
      this.setData({
        isShowSearchResult: false
      })
    }

    this.setData({
      isShowSearchHistory: false,
      isShowNull: false
    })
  },

  // 搜索框 - 点击清空控件时触发
  onSearchClear() {
    this.setData({
      searchVal: '',
      isShowSearchHistory: false,
      isShowSearchResult: false,
      isShowNull: false
    })
  },

  // 搜索框 - 输入内容变化时触发
  onSearchChange(event) {
    this.setData({
      searchVal: event.detail
    })
  },

  // 搜索框 - 确认搜索时
  onSearch(e) {
    if (this.data.searchVal !== '') {
      this.setData({
        isShowSearchHistory: false,
        isShowSearchResult: true,
        isShowNull: false
      })
      keywordModel.addToHistory('search_street_history', this.data.searchVal)
      this.httpGetYddStores()
    }
  },

  //  历史搜索
  onHistorySearch(event) {
    this.setData({
      searchVal: event.currentTarget.dataset.value,
      isShowSearchHistory: false,
      isShowSearchResult: true,
      isShowNull: false
    })
    this.httpGetYddStores()
  },

  // 清空历史记录
  onClearHistory() {
    this.setData({
      searchHistory: []
    })
    wx.setStorageSync('search_street_history', [])
  },

  // 重新定位
  onRefreshLocation() {
    this.httpWxGetLocation()
  },

  // 街道
  onStreet(event) {
    const {
      index
    } = event.currentTarget.dataset
    const {
      isShowSearchHistory,
      isShowSearchResult
    } = this.data
    if (!(isShowSearchHistory || isShowSearchResult)) {
      console.log('near', this.data.nearStreet[index])
      wx.setStorageSync('street_info', this.data.nearStreet[index])
    } else {
      console.log('search', this.data.searchList[index])
      wx.setStorageSync('street_info', this.data.searchList[index])
    }
    wx.navigateTo({
      url: '/pages/home/streetDetails/streetDetails?isStreetInfo=true'
    });
  },

  // 获取巷友门店
  httpGetYddStores() {
    return homeModel.getyddSearchServicePointr({
      "lng": this.data.locationInfo.longitude || 0, //经度
      "lat": this.data.locationInfo.latitude || 0, //纬度
      "query_v": this.data.searchVal //返回数量
    }).then((res) => {
      if (this.data.searchVal === '') {
        this.setData({
          nearStreet: res.service_point.length === 0
            ? this.data.nearStreet
            : res.service_point
        })
      } else {
        if (res.service_point.length === 0) {
          this.setData({
            isShowNull: true
          })
        } else {
          this.setData({
            searchList: res.service_point
          })
        }
      }
    })
  },

  // 获取地理位置信息
  httpWxGetLocation() {
    let self = this
    return new Promise((resolve, reject) => {
      wx.getLocation({
        type: 'wgs84',
        success: function(res) {
          self.setData({
            locationInfo: {
              longitude: res.longitude,
              latitude: res.latitude
            }
          })
          self.httpGetYddStores()
          resolve(res)
        },
        fail: function(err) {
          reject(err)
        }
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})
